package com.mybook.app.core;

import org.apache.commons.lang3.StringUtils;

import java.util.Optional;

/**
 * Created by hdl on 2017/5/9.
 */
public final class UserContext {
    private UserContext() {

    }

    private static UserContext userContext = new UserContext();

    private ThreadLocal<String> userId = new ThreadLocal<>();

    public static void init(String userId) {

        if (StringUtils.isNotBlank(userId)) {
            userContext.userId.set(userId);
        }
    }


    public static Optional<String> getCurrentuserId() {
        if (null != userContext.userId.get()) {
            return Optional.of(userContext.userId.get());
        }
        return Optional.of("-1");
    }

    public static void clean() {
        userContext.userId.remove();
    }
}
